|
|
|
|
|
|
|
The main difference is that the start parameter is located after the strings, not before. Using InstrRev, you can modify the code in Listing 9.4 slightly to come up with a new sub procedure, PrintWordsReverse. |
|
|
|
|
|
|
|
|
Sub PrintWordsReverse(stInput As String)
Dim inCounter As Integer
Dim inFoundPos As Integer
Const PARSECHAR = " " 'Space
'If string is blank then do nothing
If Len(stInput) = 0 Then Exit Sub
'Start at the last character
inCounter = Len(stInput)
'Search for a space
inFoundPos = InStrRev(stInput, PARSECHAR, inCounter)
'If a space is found print the word and keep searching
While inFoundPos <> 0
Debug.Print Mid$(stInput, inFoundPos + 1, inCounter - inFoundPos)
inCounter = inFoundPos - 1
inFoundPos = InStrRev(stInput, PARSECHAR, inCounter)
Wend
'Print the remainder of the string
If inCounter > 0 Then
Debug.Print Left$(stInput, inCounter)
End If
End Sub |
|
|
|
|
|
|
|
|
Extracting Pieces of a String |
|
|
|
|
|
|
|
|
Look at the code from Listing 9.3 again. In addition to using just the UCase and LCase functions, it also uses several functions to extract pieces of text from the original string. |
|
|
|
|
|
|
|
|
You will find many situations in which you need to work with only part of a string. Perhaps you need to extract the first name of a person from the full name, or maybe you need to make sure that the information you're working with fits in the database field in which it needs to be stored. You can easily accomplish this task by using one of the following Visual Basic functions: |
|
|
|
|
|
|
|
|
LeftRetrieves a specified number of characters from the left end of a string |
|
|
|
|
|
|
|
|
RightRetrieves a specified number of characters from the right end of a string |
|
|
|
|
|
|
|
|
MidRetrieves characters from the middle of a string |
|
|
|
|
|
|
|
|
First, look at the Left and Right functions, as they are slightly easier to use. (By the way, none of these functions is hard to use.) To use these functions, you specify the input string and the number of characters to be retrieved. The syntax of these two statements is shown in the following lines: |
|
|
|
|
|
|
|
|
OutStr = Left$(InptStr, NumChars)
OutStr = Right$(InptStr, NumChars) |
|
|
|
|
|